package com.chang.order.listener;

import com.chang.common.to.mq.SeckillOrderTo;
import com.chang.order.service.OrderService;
import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: changzhenxi
 * @date: 2025年05月06日 16:03
 * @Version: 1.0
 * @description: 秒杀队列监听
 */
@RabbitListener(queues = "order.seckill.queue")
@Component
@Slf4j
public class OrderSeckillListener {

    @Resource
    OrderService orderService;

    @RabbitHandler
    public void listener(SeckillOrderTo seckillOrderTo, Channel channel, Message message) throws IOException {
        log.info("监听到秒杀队列信息:{}", seckillOrderTo);
        try {
            orderService.creatSeckillOrder(seckillOrderTo);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //拒绝消息
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
